package cn.iocoder.yudao.module.wine.service.supplycount;

import cn.iocoder.yudao.module.wine.controller.app.supplycount.vo.AppSupplyCountPageReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplycount.vo.AppSupplyCountRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.wine.controller.admin.supplycount.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplycount.SupplyCountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplycount.SupplyCountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 酒类行情日K Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyCountServiceImpl implements SupplyCountService {

    @Resource
    private SupplyCountMapper supplyCountMapper;

    @Override
    public Long createSupplyCount(SupplyCountSaveReqVO createReqVO) {
        // 插入
        SupplyCountDO supplyCount = BeanUtils.toBean(createReqVO, SupplyCountDO.class);
        supplyCountMapper.insert(supplyCount);
        // 返回
        return supplyCount.getId();
    }

    @Override
    public void updateSupplyCount(SupplyCountSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyCountExists(updateReqVO.getId());
        // 更新
        SupplyCountDO updateObj = BeanUtils.toBean(updateReqVO, SupplyCountDO.class);
        supplyCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyCount(Long id) {
        // 校验存在
        validateSupplyCountExists(id);
        // 删除
        supplyCountMapper.deleteById(id);
    }

    private void validateSupplyCountExists(Long id) {
        if (supplyCountMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SupplyCountDO getSupplyCount(Long id) {
        return supplyCountMapper.selectById(id);
    }

    @Override
    public PageResult<SupplyCountDO> getSupplyCountPage(SupplyCountPageReqVO pageReqVO) {
        return supplyCountMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<AppSupplyCountRespVO> appGetSupplyCountPage(AppSupplyCountPageReqVO pageReqVO) {
        return supplyCountMapper.appSelectPage(pageReqVO);

    }


}
